package query

import (
	"errors"
	"ssadmin/common/query"
	"time"
)

type MenuQuery struct {
	query.Pagination `search:"-"`
	Systemid         string   `form:"-" search:"type:exact;column:systemid;table:sys_menu"`
	CreatorEq        string   `search:"type:exact;column:creator_id;table:sys_menu"`
	ParentId         string   `form:"parentId"  search:"type:exact;column:parent_id;table:sys_menu"`
	MenuId           string   `form:"menuId"  search:"type:exact;column:id;table:sys_menu"`
	MenuIdList       []string `form:"menuIdList"  search:"type:in;column:id;table:sys_menu"`
	Type             string   `form:"type"  search:"type:exact;column:type;table:sys_menu"`
	MenuName         string   `form:"menuName"  search:"type:contains;column:menu_name;table:sys_menu"`
	Search           string   `form:"search"  search:"type:contains;column:menu_name;table:sys_menu"`
	Enable           string   `form:"enable"  search:"type:exact;column:enable;table:sys_menu"`
	Filter           string   `form:"filter"  search:"-"`
	MenuOrder

	Username    string `form:"username"  search:"-"`
	UserId      string `form:"userId"  search:"-"`
	ManageQuery bool   `form:"manageQuery" search:"-"`
}

func (m *MenuQuery) GetNeedSearch(filter string, menuIds ...string) interface{} {
	if filter == "" || filter == "1" {
		m.Enable = "1"
	}

	if len(menuIds) > 0 {
		m.MenuIdList = menuIds
	}
	return *m
}

func (e *MenuQuery) GetUsername() string {
	return e.Username
}

type MenuOrder struct {
	Path       string    `form:"pathOrder"  search:"type:order;column:path;table:sys_menu"`
	Type       string    `form:"typeOrder"  search:"type:order;column:type;table:sys_menu"`
	OrderNum   string    `form:"orderNumOrder"  search:"type:order;column:order_num;table:sys_menu"`
	CreateTime time.Time `form:"createTimeOrder"  search:"type:order;column:create_time;table:sys_menu"`
	ModifyTime time.Time `form:"modifyTimeOrder"  search:"type:order;column:modify_time;table:sys_menu"`
	Level      string    `form:"levelOrder"  search:"type:order;column:level;table:sys_menu"`
	Title      string    `form:"titleOrder"  search:"type:order;column:title;table:sys_menu"`
}

func (m *MenuQuery) GetId() (id interface{}, err error) {
	if len(m.MenuId) == 0 {
		return nil, errors.New("ID不能为空")
	}
	return *m, nil
}
